package util

import (
	"net"
	"net/http"
)

//RemoteIP 获取客户端IP地址
func RemoteIP(p *http.Request) string {
	remoteAddr := p.RemoteAddr
	if ip := p.Header.Get("X-Real-IP"); ip != "" {
		remoteAddr = ip
	} else if ip = p.Header.Get("X-Forwarded-For"); ip != "" {
		remoteAddr = ip
	} else {
		remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
	}
	if remoteAddr == "::1" {
		remoteAddr = "127.0.0.1"
	}
	return remoteAddr
}
